autocmd BufRead acwrite set ma
" goto last visited line
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
augroup CompleteModeChange
    " 离开InsertMode时，关闭补全，非paste模式
    autocmd InsertLeave * set nopaste
    " 补全完成后关闭预览窗口
    autocmd InsertLeave,CompleteDone  * if pumvisible() == 0 | pclose | endif
augroup END
" from https://github.com/antoinemadec/FixCursorHold.nvim
if has('nvim') && !has('nvim-0.8')
    let g:cursorhold_updatetime = get(g:, 'cursorhold_updatetime', 100)
    let g:fix_cursorhold_nvim_timer = -1
    set eventignore+=CursorHold,CursorHoldI
    augroup fix_cursorhold_nvim
        autocmd!
        let &t_SI = "\e[6 q"
        let &t_EI = "\e[2 q"
        autocmd CursorMoved  * call CursorHoldTimer()
        autocmd CursorMovedI * call CursorHoldITimer()
    augroup END
    function! CursorHold_Cb(timer_id) abort
        set eventignore-=CursorHold
        doautocmd <nomodeline> CursorHold
        set eventignore+=CursorHold
    endfunction
    function! CursorHoldI_Cb(timer_id) abort
        set eventignore-=CursorHoldI
        doautocmd <nomodeline> CursorHoldI
        set eventignore+=CursorHoldI
    endfunction
    function! CursorHoldTimer() abort
        call timer_stop(g:fix_cursorhold_nvim_timer)
        let g:fix_cursorhold_nvim_timer = timer_start(g:cursorhold_updatetime, 'CursorHold_Cb')
    endfunction
    function! CursorHoldITimer() abort
        call timer_stop(g:fix_cursorhold_nvim_timer)
        let g:fix_cursorhold_nvim_timer = timer_start(g:cursorhold_updatetime, 'CursorHoldI_Cb')
    endfunction
endif
" FOLDS
augroup FOLDS
    autocmd!
    autocmd FileType tex setl foldlevel=0 foldnestmax=1
    autocmd BufRead,BufNewFile *.c,*.cpp,*.cc setl foldlevel=0 foldnestmax=1
augroup END
" auto lcd current dir
augroup AUTOLCD
    " cd file dir
    autocmd WinEnter * if bufname("") !~ "^\[A-Za-z0-9\]*://"   " terminal
                \ && bufname("") !~ "Rg"                   " rg
                \ && bufname("") !~ "outline"              " outline
                \ && bufname("")[0] != "!"                 " some special buf
                \ && getbufvar(winbufnr(winnr()), "&buftype") != "popup"
                \ | lcd %:p:h | endif
augroup END
" --------------------------
" swap exists ignore
" --------------------------
autocmd SwapExists * let v:swapchoice = 'o'
" --------------------------
" file templates
" --------------------------
autocmd BufNewFile .gitignore          0r $LEOVIM_PATH/templates/gitignore.spec
autocmd BufNewFile .lintr              0r $LEOVIM_PATH/templates/lintr.spec
autocmd BufNewFile .Rprofile           0r $LEOVIM_PATH/templates/Rprofile.spec
autocmd BufNewFile .radian_profile     0r $LEOVIM_PATH/templates/radian_profile.spec
autocmd BufNewFile projectionlist.json 0r $LEOVIM_PATH/templates/projectionlist.json.spec
" --------------------------
" numbertoggle
" --------------------------
augroup numbertoggle
    autocmd!
    autocmd BufEnter,FocusGained,InsertLeave,WinEnter * if &nu && mode() != "i" | set rnu | endif
    autocmd BufLeave,FocusLost,InsertEnter,WinLeave   * if &nu | set nornu | endif
    if exists(':tnoremap')
        autocmd CmdlineLeave * if &nu && mode() != "i" | set rnu | endif
        autocmd CmdlineEnter * if &nu | set nornu | endif
    endif
    if has('nvim')
        autocmd TermLeave,TermClose * if &nu && mode() != "i" | set rnu | endif
        autocmd TermEnter,TermOpen  * if &nu | set nornu | endif
    endif
augroup END
" -----------------------------------
" hightlight todo note
" -----------------------------------
augroup SPECIALSTINGS
    autocmd!
    autocmd Syntax * call matchadd('Todo', '\v\W\zs' . g:todo_patterns . '(\(.{-}\))?:?', -1)
    autocmd Syntax * call matchadd('Todo', '\v\W\zs' . g:note_patterns . '(\(.{-}\))?:?', -2)
augroup END
" -----------------------------------
" not automatical add comments whne o/O
" -----------------------------------
augroup NoAddComment
    autocmd!
    autocmd FileType * set formatoptions-=c formatoptions-=r formatoptions-=o
augroup END
